<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="圈子名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['groupName',{rules: [{ required: true, message: '请输入内容!', }]}]" placeholder="请输入圈子名称"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="疾病选择：">
          科室：
          <a-select v-model="departmentId" placeholder="请选择" @change="updateDepartment"
                    style="width: 120px">
            <a-select-option v-for="item in departmentList" :value="item.id" :key="item.id">
              {{item.departmentName}}
            </a-select-option>
          </a-select>
          疾病：
          <a-select v-model="diseaseId" placeholder="请选择" @change="selectDisease"
                    style="width: 120px">
            <a-select-option v-for="item in diseaseList" :value="item.id" :key="item.name">{{item.diseaseName}}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="对应疾病" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            mode="multiple"
            v-model="diseaseInfoList"
            style="width: 100%"
            placeholder="请选择疾病"
            @change="handleChange">
            <a-select-option v-for="item in diseaseSelectList"
                             :value="item.id"
                             :key="item.id">
              {{ item.diseaseName }}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item label="图标" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <demo-upload v-model="model.iconUrl"></demo-upload>
        </a-form-item>
        <a-form-item label="简介" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['groupSynopsis']" placeholder="请输入简介"></a-textarea>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import {getAction, httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {validateDuplicateValue} from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import ATextarea from "ant-design-vue/es/input/TextArea";
  import DemoUpload from '@/components/jeecg/DemoUpload'


  export default {
    name: "PusGroupInfoModal",
    components: {
      ATextarea,
      DemoUpload,
      JDate,
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title: "操作",
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        validatorRules: {},
        url: {
          add: "/group/pusGroupInfo/add",
          edit: "/group/pusGroupInfo/edit",
          getDepartmentList: "/hospital/medicalDepartmentInfo/getListByGrade",
          getDiseaseList: "/hospital/medicalDiseaseInfo/getListByDepartmentId",
        },
        departmentId: "",
        diseaseId: "",
        departmentList: [],
        diseaseInfoList: [],
        diseaseList: [],
        diseaseSelectList: [],
      }
    },
    created() {
    },
    mounted() {
      this.initIndex();
    },
    methods: {
      initIndex() {
        //科室
        getAction(this.url.getDepartmentList).then((res) => {
          if (res.success) {
            this.departmentList = res.result;
          }
        });
        this.getDiseaseList();
      },
      getDiseaseList(value) {
        //获取科室下的疾病
        getAction(this.url.getDiseaseList, {departmentId: value}).then((res) => {
          if (res.success) {
            this.diseaseList = res.result;
          }
        });
      },
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        this.departmentId = "";
        this.diseaseId = "";
        this.diseaseSelectList = [];
        this.diseaseInfoList = [];
        this.model.iconUrl = "";
        console.log("打印model：{}",this.model);
        if (record.id) {
          this.diseaseInfoList = record.diseaseInfoList;
          //对应疾病列表
          this.diseaseSelectList = record.diseaseSelectList;
        }
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'groupName', 'diseaseId', 'departmentId', 'groupNumber', 'createBy', 'createTime', 'updateBy', 'updateTime', 'delFlag', 'diseaseInfoList', 'diseaseSelectList'))
        })
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            this.model.diseaseInfoList = this.diseaseInfoList;
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据", formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel() {
        this.close()
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'groupName', 'diseaseId', 'departmentId', 'groupNumber', 'createBy', 'createTime', 'updateBy', 'updateTime', 'delFlag', 'diseaseInfoList'))
      },
      handleChange(value) {
        this.diseaseInfoList = value;
      },
      selectDisease(value) {
        this.diseaseList.forEach(a => {
          if (a.id === value) {
            this.diseaseSelectList.push(a);
          }
        })
        this.diseaseInfoList.push(value);
      },
      updateDepartment(value) {
        //查询科室下的疾病列表
        getAction(this.url.getDiseaseList, {departmentId: value}).then((res) => {
          if (res.success) {
            this.diseaseList = res.result;
          }
        });
      },

    }
  }
</script>